import { performance } from 'perf_hooks';
import { RedpackClaim } from './request/api.js';


const concurrency = 1; // 并发数
const iterations = 1; // 测试次数

const fromAddress = '0xA55F5719A18af522085ee80A3f0e4286EeE2f231'; // 设置参数

const startTime = performance.now();

let completedRequests = 0;
let totalRequestTime = 0;

async function runConcurrentDepositRedPackRequests() {
  const promises = [];
  for (let i = 0; i < concurrency; i++) {
    promises.push(runSingleDepositRedPack(i));
  }
  await Promise.all(promises);
}

async function runSingleDepositRedPack(index) {
  for (let i = index * iterations  ; i < index * iterations + iterations; i++) {
    try {
      // 调用要测试的方法
      const startRequestTime = performance.now();
      const amount = "1"; // 设置参数
      const request = {
        claimAddress: fromAddress,
        value: Number(amount),
      };

      RedpackClaim(request).then(() => {
        const endRequestTime = performance.now();
        const requestTime = endRequestTime - startRequestTime;
        console.log("sendTx = ", requestTime)
        totalRequestTime += requestTime;
        console.log(`Request ${index + 1} - Iteration ${i + 1} successful. Result:`);
        completedRequests++;

        if(completedRequests === concurrency * iterations){
          const endTime = performance.now();
          const totalTime = endTime - startTime;
          const tps = completedRequests / (totalTime / 1000);
          const averageRequestTime = totalRequestTime / completedRequests;
          console.log(`All requests completed in ${totalTime} milliseconds.`);
          console.log(`Average request time: ${averageRequestTime} milliseconds.`);
          console.log(`TPS: ${tps}`);
        }

      });
    } catch (error) {
      console.error(`Request ${index + 1} - Iteration ${i + 1} failed:`, error);
    }
  }
}


runConcurrentDepositRedPackRequests().then(() => {
   
});




